package com.reformer.commons.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 时间工具类
 *
 * @author Administrator
 */
public class TimeUtil {

    public static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static final String CONCISE_PATTERN = "yyyyMMddHHmmss";
    public static final String CONCISE_PATTERN_MS = "yyyyMMddHHmmssSSS";
    /**
     * yyyy-MM-dd HH:mm:ss
     */
    public static final DateFormat DEFAULT_DATEFORMAT = new SimpleDateFormat(DEFAULT_PATTERN);
    /**
     * yyyyMMddHHmmss
     */
    public static final DateFormat CONCISE_DATEFORMAT = new SimpleDateFormat(CONCISE_PATTERN);
    /**
     * yyyyMMddHHmmssSSS
     */
    public static final DateFormat CONCISE_DATEFORMAT_MS = new SimpleDateFormat(CONCISE_PATTERN_MS);

    /**
     * 计算两个日期之间的时间差
     *
     * @param unit  时间单位
     * @param date1
     * @param date2
     * @return
     */
    public static long timeDiff(TimeUnit unit, Date date1, Date date2) {
        long time1 = date1.getTime();
        long time2 = date2.getTime();
        long diff = Math.abs(time1 - time2);
        switch (unit) {
            case DAY:
                return diff / 1000 / 60 / 60 / 24;
            case HOUR:
                return diff / 1000 / 60 / 60;
            case MINUTE:
                return diff / 1000 / 60;
            case SECOND:
                return diff / 1000;
            case MILLIS_SECOND:
                return diff;
            default:
                throw new RuntimeException("参数有误");
        }
    }

    /**
     * 在数字前面加0
     *
     * @param n
     * @return
     */
    public static String addZero(int n) {
        if (n < 0) {
            return "00";
        } else if (n < 10) {
            return "0" + n;
        }
        return "" + n;
    }

    /**
     * pattern: yyyy-MM-dd HH:mm:ss
     *
     * @param date
     * @return
     */
//	public static String formatByDefaultPattern(Date date) {
//		return DEFAULT_DATEFORMAT.format(date);
//	}

    /**
     * pattern: yyyyMMddHHmmss
     *
     * @param date
     * @return
     */
//	public static String formatByConcisePattern(Date date) {
//		return CONCISE_DATEFORMAT.format(date);
//	}
//	public static String formatByConcisePatternMS(Date date) {
//		return CONCISE_DATEFORMAT_MS.format(date);
//	}
    /**
     * 已指定的格式来格式化日期
     * @param date
     * @param pattern
     * @return
     */
//	public static String format(Date date,String pattern) {
//		return new SimpleDateFormat(pattern).format(date);
//	}

    /**
     * 以yyyy->MM->dd->HH->mm->ss的顺序解析日子
     *
     * @param pattern
     * @param source
     * @return
     */
//	public static Timestamp parse(String source) {
//		try {
//			return new Timestamp(CONCISE_DATEFORMAT.parse(source.replaceAll("\\D", "")).getTime());
//		} catch (Exception e) {
//			return new Timestamp(new Date().getTime());
//		}
//	}
    /**
     * 以指定的格式来解析日期
     * @param source
     * @param pattern
     * @return
     */
//	public static Timestamp parse(String source,String pattern) {
//		try {
//			return new Timestamp(new SimpleDateFormat(pattern).parse(source).getTime());
//		} catch (Exception e) {
//			return new Timestamp(new Date().getTime());
//		}
//	}

    /**
     * 计算时长 如：x天x小时x分x秒
     *
     * @param durationOfSecond
     * @return
     */
    public static String formatByDuration(long durationOfSecond) {
        long day = durationOfSecond / (24 * 3600);
        long hour = durationOfSecond % (24 * 3600) / 3600;
        long minute = durationOfSecond % (24 * 3600) % 3600 / 60;
        long second = durationOfSecond % (24 * 3600) % 3600 % 60;
        StringBuffer sb = new StringBuffer();
        if (day > 0) {
            sb.append(day + "天");
        }
        if (hour > 0) {
            sb.append(hour + "小时");
        }
        if (minute > 0) {
            sb.append(minute + "分");
        }
        if (second > 0) {
            sb.append(second + "秒");
        }
        return sb.toString();
    }

    public static enum TimeUnit {
        DAY, HOUR, MINUTE, SECOND, MILLIS_SECOND
    }
}
